//
//  GYGPMainController.m
//  cocoapodText
//
//  Created by apple on 16/9/7.
//  Copyright © 2016年 apple. All rights reserved.
//


#import "GYGPMainController.h"
#import <Foundation/Foundation.h>
@interface GYGPMainController ()
@property(nonatomic,strong) UIView *GPContentView;
@end

@implementation GYGPMainController

GTabChildBaseController *tabChildBaseController;
InfoOverviewController *informationSurveyController;
EnergyConsumptionAnalysis *energyConsumptionAnalysis;
EnergyConsumptionBenchmarking *energyConsumptionBenchmarking;
QuotaAnalysis *quotaAnalysis;
EnergyDispatch *energyDispatch;
EnergySavingFootprint *energySavingFootprint;
MessageSubscription *messageSubscription;
PasswordMaintenanceNew *passwordMaintenance;
//公共标题
FSCommonTitle *commonTitle;
//左侧菜单栏
GPDock *dock;
bool transiting;
UIViewController *currentViewController;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [self SetUpStatusBar];
    [self GetMenuAndContentView];
    [self GetChildControllers];
    [self GetTitleView];
    [self GetBottomLogoView];
}

-(void)SetUpStatusBar{
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 20)];
    statusBarView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:statusBarView];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}

-(void)GetTitleView{
    commonTitle=[[FSCommonTitle alloc]initWithFrame:CGRectMake(0, 0, kScreenW, [ShiPei CGFloatMakeScale:CommonBarH])];
    [commonTitle setDelegate:self];
    [commonTitle setTitleText:@"信息概览"];
    [self.view addSubview:commonTitle];
}

-(void)GetMenuAndContentView{
    //加入侧边栏Dock
    dock=[[GPDock alloc]initWithFrame:CGRectMake(0, kStatusH,[ShiPei CGFloatMakeScale:GPDockItemWidth], kScreenH)];
    dock.dockDelegate=self;
    [self.view addSubview:dock];
    //菜单与内容竖直分割线
    //内容栏
    _GPContentView=[[UIView alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:GPDockItemWidth], [ShiPei CGFloatMakeScale:CommonBarH+kStatusH], kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth], kScreenH-[ShiPei CGFloatMakeScale:50]-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [_GPContentView setUserInteractionEnabled:YES];
    [_GPContentView setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    //边框
    [_GPContentView.layer setBorderWidth:2.5];
    [_GPContentView.layer setBorderColor:[[FUIColorFromRGB(0xe5e5e5)colorWithAlphaComponent:0.1] CGColor]];
    [self.view addSubview:_GPContentView];
}

-(void)GetBottomLogoView{
    //北京方胜有成科技股份有限公司
    //屏幕logo
    UIView *bottomLogoView=[[UIView alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:GPDockItemWidth], kScreenH-[ShiPei CGFloatMakeScale:FSCommonBottomH], kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth], [ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [bottomLogoView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:bottomLogoView];
    
    UIImageView *fengexianImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth], 1)];
    [fengexianImageView setBackgroundColor:FUIColorFromRGB(0xdfe2e6)];
    [bottomLogoView addSubview:fengexianImageView];
    
    UILabel *appFromLabel=[[UILabel alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:20], 0, (kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth]-[ShiPei CGFloatMakeScale:20])/2, [ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [bottomLogoView addSubview:appFromLabel];
    [appFromLabel setBackgroundColor:[UIColor clearColor]];
    [appFromLabel setText:@"北京方胜有成科技股份有限公司"];
    [appFromLabel setTextColor:FUIColorFromRGB(0x333333)];
    [appFromLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:20]]];
    [appFromLabel setTextAlignment:NSTextAlignmentLeft];
    
    UILabel *appVersionLabel=[[UILabel alloc]initWithFrame:CGRectMake((kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth])/2, 0, (kScreenW-[ShiPei CGFloatMakeScale:GPDockItemWidth]-[ShiPei CGFloatMakeScale:30])/2, [ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [bottomLogoView addSubview:appVersionLabel];
    [appVersionLabel setBackgroundColor:[UIColor clearColor]];
    [appVersionLabel setText:@"Version 1.0.0"];
    [appVersionLabel setTextColor:FUIColorFromRGB(0x333333)];
    [appVersionLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:20]]];
    [appVersionLabel setTextAlignment:NSTextAlignmentRight];
    
}

-(void)GetChildControllers{
    //添加子Controller
    informationSurveyController=[[InfoOverviewController alloc]init];
    [self addChildViewController:informationSurveyController];
    energyConsumptionAnalysis=[[EnergyConsumptionAnalysis alloc]init];
    [self addChildViewController:energyConsumptionAnalysis];
    quotaAnalysis=[[QuotaAnalysis alloc]init];
    [self addChildViewController:quotaAnalysis];
    energyConsumptionBenchmarking=[[EnergyConsumptionBenchmarking alloc]init];
    [self addChildViewController:energyConsumptionBenchmarking];
    energyDispatch=[[EnergyDispatch alloc]init];
    [self addChildViewController:energyDispatch];
    energySavingFootprint=[[EnergySavingFootprint alloc]init];
    [self addChildViewController:energySavingFootprint];
    messageSubscription=[[MessageSubscription alloc]init];
    [self addChildViewController:messageSubscription];
    passwordMaintenance=[[PasswordMaintenanceNew alloc]init];
    [self addChildViewController:passwordMaintenance];
    //显示默认子Controller
    [_GPContentView addSubview:informationSurveyController.view];
    currentViewController=informationSurveyController;
}

-(void)onWarnInfoClick:(FSCommonTitle *)warninfo{
    [CommonTool updateValueWithKey:@"MessageSubscriptionAction" value:warnInfo];
    if (currentViewController!=messageSubscription) {
        //消息订阅页不是当前页
        [dock ckickGDDockWithTab:6];
    }else{
        //发广播通知消息订阅更新数据
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageSubscriptionAction" object:nil];
    }
}

-(void)onMessageClick:(FSCommonTitle *)message{
    [CommonTool updateValueWithKey:@"MessageSubscriptionAction" value:newMessage];
    [dock ckickGDDockWithTab:6];
}

-(void)onUserImageClick:(FSCommonTitle *)userimage{
    CGRect frame=userimage.TitleUserInfoImageView.frame;
    frame.origin.x=950+frame.origin.x;
    frame.origin.y=20+frame.origin.y;
    [self userimage:frame];
}



-(void)switchMainByTabItem:(GPDock *)gpdock originalTab:(int)start destinationTab:(int)end{
    if((currentViewController==passwordMaintenance&&end==7)||(currentViewController==messageSubscription&&end==6)||(currentViewController==energySavingFootprint&&end==5)||(currentViewController==energyDispatch&&end==4)||(currentViewController==energyConsumptionAnalysis&&end==1)||(currentViewController==energyConsumptionBenchmarking&&end==2)||(currentViewController==quotaAnalysis&&end==3)||(currentViewController==informationSurveyController&&end==0)) {
        return;
        //NSLog(@"最后选择的页面是：end=%d",end);
    }
    if (transiting) {
        return;
    }
    transiting = YES;
    UIViewController *oldViewController=currentViewController;
    
    switch (end) {
        case 0:
        {
            [commonTitle setTitleText:@"信息概览"];
            [self transitionFromViewController:currentViewController toViewController:informationSurveyController duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=informationSurveyController;
                }else{
                    currentViewController=oldViewController;
                }
                 transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            break;
        case 1:
        {
            [commonTitle setTitleText:@"能耗分析"];
            [self transitionFromViewController:currentViewController toViewController:energyConsumptionAnalysis duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=energyConsumptionAnalysis;
                }else{
                    currentViewController=oldViewController;
                }
                transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            break;
        case 2:
        {
            [commonTitle setTitleText:@"能耗对标"];
            [self transitionFromViewController:currentViewController toViewController:energyConsumptionBenchmarking duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=energyConsumptionBenchmarking;
                }else{
                    currentViewController=oldViewController;
                }
                transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            
            break;
        case 3:
        {
            [commonTitle setTitleText:@"定额分析"];
            [self transitionFromViewController:currentViewController toViewController:quotaAnalysis duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=quotaAnalysis;
                }else{
                    currentViewController=oldViewController;
                }
                transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            break;
        case 4:
        {
            [commonTitle setTitleText:@"能源调度"];
            [self transitionFromViewController:currentViewController toViewController:energyDispatch duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=energyDispatch;
                }else{
                    currentViewController=oldViewController;
                }
                transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            break;
        case 5:
        {
            [commonTitle setTitleText:@"节能足迹"];
            [self transitionFromViewController:currentViewController toViewController:energySavingFootprint duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=energySavingFootprint;
                }else{
                    currentViewController=oldViewController;
                }
                transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            break;
        case 6:
        {
            [commonTitle setTitleText:@"消息订阅"];
            [self transitionFromViewController:currentViewController toViewController:messageSubscription duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=messageSubscription;
                }else{
                    currentViewController=oldViewController;
                }
                transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            break;
        case 7:
        {
            [commonTitle setTitleText:@"密码维护"];
            [self transitionFromViewController:currentViewController toViewController:passwordMaintenance duration:0.1 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                    currentViewController=passwordMaintenance;
                }else{
                    currentViewController=oldViewController;
                }
                transiting = NO;
                [gpdock changeTabItemStyle:end];
            }];
        }
            break;
        
        default:
            transiting = NO;
            [gpdock changeTabItemStyle:end];
            break;
    }
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //更新预警信息
    [commonTitle refreshWarnInfo];
    //消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(BackLogin) name:@"SessionExpired" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(BackLoginError) name:@"UncaughtExceptionHandler" object:nil];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //用来制造一些异常
    //[self performSelector:@selector(string) withObject:nil afterDelay:2.0];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //消息
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"SessionExpired" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UncaughtExceptionHandler" object:nil];
}

-(void)BackLogin{
    UIAlertView *SessionExpired=[[UIAlertView alloc]initWithTitle:@"提示" message:@"登录已过期，请重新登录！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [SessionExpired show];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)BackLoginError{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)userimage:(CGRect )frame{
    NSMutableArray *models=[NSMutableArray array];
    KxMenuItem *model;
    //月=2 年=3
    model=[KxMenuItem menuItem:UserPass image:[UIImage imageNamed:@"fs_usermenu_pass.png"] target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:UserLoginOut image:[UIImage imageNamed:@"fs_usermenu_loginout.png"] target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    [KxMenu setTitleFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    Color color,color2;
    color.R=0;
    color.G=0;
    color.B=0;
    color2.R=1;
    color2.G=1;
    color2.B=1;
    OptionalConfiguration options;
    options.arrowSize=9;//指示箭头大小
    options.marginXSpacing= 7;  //MenuItem左右边距
    options.marginYSpacing= 9;  //MenuItem上下边距
    options.intervalSpacing= 30;  //MenuItemImage与MenuItemTitle的间距
    options.menuCornerRadius= 3;  //菜单圆角半径
    options.maskToBackground= true;  //是否添加覆盖在原View上的半透明遮罩
    options.shadowOfMenu= false;  //是否添加菜单阴影
    options.hasSeperatorLine= true;  //是否设置分割线
    options.seperatorLineHasInsets= true;  //是否在分割线两侧留下Insets
    options.textColor=color;  //menuItem字体颜色
    options.menuBackgroundColor= color2;  //菜单的底色
    [KxMenu showMenuInView:self.view fromRect:frame menuItems:models withOptions:options];
    
}

-(void)KxMenuItemClick:(id)sender{
    KxMenuItem *model=sender;
    if ([model.title isEqualToString:UserPass]) {
        [dock ckickGDDockWithTab:7];
    }else if([model.title isEqualToString:UserLoginOut]){
        UserLogin *loginmanager=[UserLogin alloc];
        [loginmanager setDelegate:self];
        [loginmanager fsgy_api_user_logout_withName:[CommonTool getValueFromKey:@"username" defaultValue:@""] Pass:[CommonTool getValueFromKey:@"userpass" defaultValue:@""]];
        [CommonTool updateValueWithKey:@"is_login" value:@"0"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(void)onLogin:(UserLogin *)manager result:(NSDictionary *)result{
    
}

-(void)onLogout:(UserLogin *)manager result:(NSDictionary *)result{
    NSString *logonCode=[result objectForKey:@"logonCode"];
    if ([logonCode isEqualToString:@"2"]){
        //注销成功
        [CommonTool showAllTextDialogWithText:@"退出成功" parentView:self.view];
    }
}

@end
